package com.caozhi.mspeoject.nb_search.controller;

import com.caozhi.mspeoject.nb_search.repository.FeedbackSearchRepository;
import com.woniuxy.boot.common.bean.EsFeedback;
import com.woniuxy.boot.common.dto.EsFeedbackSearchDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("feedbackSearch")
public class FeedbackSearchController {
    @Autowired
    private FeedbackSearchRepository feedbackSearchRepository;

    @PostMapping("get")
    public List<EsFeedback> getFeedback(@RequestBody EsFeedbackSearchDTO feedbackSearch) {
        System.out.println(feedbackSearch);
        List<EsFeedback> esFeedbackList = new ArrayList<>();
        if(feedbackSearch.getName() != null&& !feedbackSearch.getName().isEmpty()){
            esFeedbackList = feedbackSearchRepository.findByName(feedbackSearch.getName());
        }else if(feedbackSearch.getPhone()!=null && !feedbackSearch.getPhone().isEmpty()){
            esFeedbackList = feedbackSearchRepository.findByPhone(feedbackSearch.getPhone());
        }else {
            esFeedbackList = feedbackSearchRepository.findAll();
        }
        System.out.println("Es查询结果"+esFeedbackList);

        if (feedbackSearch.getIssue() != null && !feedbackSearch.getIssue().isEmpty()) {
            esFeedbackList = esFeedbackList.stream().filter(esFeedback -> esFeedback.getIssue().equals(feedbackSearch.getIssue()))
                    .toList();
        }
        if (feedbackSearch.getIsDispose() != null && !feedbackSearch.getIsDispose().isEmpty()) {
            esFeedbackList = esFeedbackList.stream().filter(esFeedback -> esFeedback.getIsDispose().equals(feedbackSearch.getIsDispose()))
                    .toList();
        }
        if (feedbackSearch.getStartTime() != null && feedbackSearch.getEndTime() != null) {
            esFeedbackList = esFeedbackList.stream().filter(esFeedback -> esFeedback.getCommitTime().isAfter(feedbackSearch.getStartTime()) && esFeedback.getCommitTime().isBefore(feedbackSearch.getEndTime()))
                    .toList();
        } else if (feedbackSearch.getStartTime() != null) {
            esFeedbackList = esFeedbackList.stream().filter(esFeedback -> esFeedback.getCommitTime().isAfter(feedbackSearch.getStartTime()))
                    .toList();
        } else if (feedbackSearch.getEndTime() != null) {
            esFeedbackList = esFeedbackList.stream().filter(esFeedback -> esFeedback.getCommitTime().isBefore(feedbackSearch.getEndTime()))
                    .toList();
        }
        System.out.println(esFeedbackList);
        return esFeedbackList;
    }
}
